<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/blog/lib/font-awesome/css/font-awesome.min.css">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    hostname: new URL('https://guodh.gitee.io/blog').hostname,
    root: '/blog/',
    scheme: 'Pisces',
    version: '7.7.1',
    exturl: false,
    sidebar: {"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},
    copycode: {"enable":true,"show_result":true,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    comments: {"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.xml',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}
  };
</script>

  <meta name="description" content="最近开启完全版996节奏来做新的项目，还是累的一比。但是，过程中涉及了好多的点还是很有价值来思考的。甚至是对于Dubbo，我都有了一个重新的认识。所以是，每次到了关键的时候才知道自己是多么的“菜”。 主要涉及的点：  Dubbo提供Hessian 网关如何支持MultipartRequest，以及是否支持dubbo的 InpuStream 传参(后来还是没有解决改成了byte[])">
<meta property="og:type" content="article">
<meta property="og:title" content="Dubbo的protocol">
<meta property="og:url" content="https://guodh.gitee.io/blog/2019/04/26/2019/Dubbo%E7%9A%84protocol/index.html">
<meta property="og:site_name" content="蛋蛋的小屋">
<meta property="og:description" content="最近开启完全版996节奏来做新的项目，还是累的一比。但是，过程中涉及了好多的点还是很有价值来思考的。甚至是对于Dubbo，我都有了一个重新的认识。所以是，每次到了关键的时候才知道自己是多么的“菜”。 主要涉及的点：  Dubbo提供Hessian 网关如何支持MultipartRequest，以及是否支持dubbo的 InpuStream 传参(后来还是没有解决改成了byte[])">
<meta property="og:locale">
<meta property="og:image" content="https://guodh.gitee.io/普通RPC.jpg">
<meta property="og:image" content="https://guodh.gitee.io/dubbo流程.jpg">
<meta property="article:published_time" content="2019-04-26T12:41:20.000Z">
<meta property="article:modified_time" content="2019-04-27T13:22:07.000Z">
<meta property="article:author" content="RunningEgg">
<meta property="article:tag" content="技术文档">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://guodh.gitee.io/普通RPC.jpg">

<link rel="canonical" href="https://guodh.gitee.io/blog/2019/04/26/2019/Dubbo%E7%9A%84protocol/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true
  };
</script>

  <title>Dubbo的protocol | 蛋蛋的小屋</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">蛋蛋的小屋</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">来了就看看吧，没有刀剑只有故事</p>
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-fw fa-home"></i>Home</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-fw fa-user"></i>About</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>Archives</a>

  </li>
        <li class="menu-item menu-item-book">

    <a href="/blog/book/" rel="section"><i class="fa fa-fw fa-archive"></i>book</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="https://guodh.gitee.io/blog/2019/04/26/2019/Dubbo%E7%9A%84protocol/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="RunningEgg">
      <meta itemprop="description" content="写下我的点点滴滴，写下我对这个世界的看法，还有我内心锁崇拜的技术">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="蛋蛋的小屋">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Dubbo的protocol
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2019-04-26 20:41:20" itemprop="dateCreated datePublished" datetime="2019-04-26T20:41:20+08:00">2019-04-26</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2019-04-27 21:22:07" itemprop="dateModified" datetime="2019-04-27T21:22:07+08:00">2019-04-27</time>
              </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>最近开启完全版996节奏来做新的项目，还是累的一比。但是，过程中涉及了好多的点还是很有价值来思考的。甚至是对于Dubbo，我都有了一个重新的认识。所以是，每次到了关键的时候才知道自己是多么的“菜”。</p>
<p>主要涉及的点：</p>
<ol>
<li>Dubbo提供Hessian</li>
<li>网关如何支持MultipartRequest，以及是否支持dubbo的 InpuStream 传参(后来还是没有解决改成了byte[])</li>
</ol>
<a id="more"></a>

<h2 id="Dubbo提供Hessian"><a href="#Dubbo提供Hessian" class="headerlink" title="Dubbo提供Hessian"></a>Dubbo提供Hessian</h2><p>由于需要提供文件服务，入参为InputStream，所以Duboo的协议需要从Dubbo改为Hessian。这里涉及的坑如下：</p>
<ol>
<li>Hessian的Port必须和Tomcat的端口一样。因为公司的Dubbo是在Tomcat容器下的，如果端口不同，会遇到 </li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Error : Connect refused</span><br></pre></td></tr></table></figure>
<ol start="2">
<li>单独的Service可以配置Protcol</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Service(version = VersionConstant.FILE_VERSION, protocol = &#123;&quot;hessian&quot;&#125;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FileInfoQueryServiceImpl</span> <span class="keyword">implements</span> <span class="title">FileInfoQueryService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这里的 hessian 是 dubbo 配置的参数 (我傻屌的改了个id名称导致一直报错)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">dubbo:</span><br><span class="line">  provider:</span><br><span class="line">    protocol: hessian</span><br><span class="line">  # 这里可以使用 protocols</span><br><span class="line">  protocol:</span><br><span class="line">  # id 不要瞎改，和注解@Service中的 protocol = &#123;&quot;hessian&quot;&#125;相关</span><br><span class="line">    id: hessian</span><br><span class="line">    # 注意这个一定要和tomcat端口相同，否则无法暴露</span><br><span class="line">    port: <span class="number">8080</span></span><br><span class="line">    name: hessian</span><br><span class="line">    server: servlet</span><br></pre></td></tr></table></figure>
<ol start="3">
<li>配置 Dubbo的 DispatcherServlet</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Bean</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> ServletRegistrationBean <span class="title">dispatcherRegistration</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    DispatcherServlet dubboDispatcherServlet = <span class="keyword">new</span> DispatcherServlet();</span><br><span class="line">    ServletRegistrationBean servletRegistrationBean = <span class="keyword">new</span> ServletRegistrationBean(dubboDispatcherServlet,<span class="string">&quot;/*&quot;</span>);</span><br><span class="line">    servletRegistrationBean.setLoadOnStartup(<span class="number">1</span>);</span><br><span class="line">    <span class="keyword">return</span> servletRegistrationBean;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="问题的思考"><a href="#问题的思考" class="headerlink" title="问题的思考"></a>问题的思考</h2><p>改一个Protcol花了我这么久的时间，那么问题到底出在哪里呢？问题的关键是我对于Dubbo不够了解清晰。对于一个框架的架构、代码，都是一知半解的状态，导致需要解决问题的时候，无从下手。</p>
<p>所以，还是画一下架构来重新认识一下。</p>
<h3 id="普通PRC的调用过程"><a href="#普通PRC的调用过程" class="headerlink" title="普通PRC的调用过程"></a>普通PRC的调用过程</h3><img src="/普通RPC.jpg" width="100%" height="100%" alt="普通RPC"/>

<h3 id="Dubbo的调用过程"><a href="#Dubbo的调用过程" class="headerlink" title="Dubbo的调用过程"></a>Dubbo的调用过程</h3><img src="/dubbo流程.jpg" width="100%" height="100%" alt="dubbo流程"/>

<p>依据 <a href="http://shiyanjun.cn/archives/325.html">http://shiyanjun.cn/archives/325.html</a> 整理</p>
<h2 id="关于网关"><a href="#关于网关" class="headerlink" title="关于网关"></a>关于网关</h2><p>业务中，产品线自研了一套API网关，用来将HTTP请求转化为Dubb的请求，</p>
<p>原来对于网关还是一个模糊的概念，现在发现关键点还是2个，一个是服务发现以及协议调用（涉及协议转化）。对于Dubbo的相关的就是如下的一些代码。</p>
<h3 id="如何拿到注册信息"><a href="#如何拿到注册信息" class="headerlink" title="如何拿到注册信息"></a>如何拿到注册信息</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// </span></span><br><span class="line">RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();</span><br><span class="line">registry = registryFactory.getRegistry(registryUrl);</span><br></pre></td></tr></table></figure>
<h3 id="如何使用泛化调用"><a href="#如何使用泛化调用" class="headerlink" title="如何使用泛化调用"></a>如何使用泛化调用</h3><p>泛化调用是指不依赖API包，通过制定Methods的方式来调用Dubbo接口</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">GenericService</span> </span>&#123;</span><br><span class="line">    Object $invoke(String method, String[] parameterTypes, Object[] args) <span class="keyword">throws</span> GenericException;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>当需要传递对象的时候，可以使用Map，提供者接收到消息的时候会是PojoUtils.java来将对象构造成对象。</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/%E6%8A%80%E6%9C%AF%E6%96%87%E6%A1%A3/" rel="tag"># 技术文档</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2019/03/27/2019/%E5%88%86%E5%B8%83%E5%BC%8F%E8%B0%83%E5%BA%A6%E4%B8%AD%E5%BF%83%E9%80%89%E5%9E%8B/" rel="prev" title="分布式调度中心选型">
      <i class="fa fa-chevron-left"></i> 分布式调度中心选型
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2019/05/10/2019/%E6%80%9D%E8%80%83%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/" rel="next" title="思考分布式系统">
      思考分布式系统 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Dubbo%E6%8F%90%E4%BE%9BHessian"><span class="nav-number">1.</span> <span class="nav-text">Dubbo提供Hessian</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%97%AE%E9%A2%98%E7%9A%84%E6%80%9D%E8%80%83"><span class="nav-number">2.</span> <span class="nav-text">问题的思考</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%99%AE%E9%80%9APRC%E7%9A%84%E8%B0%83%E7%94%A8%E8%BF%87%E7%A8%8B"><span class="nav-number">2.1.</span> <span class="nav-text">普通PRC的调用过程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Dubbo%E7%9A%84%E8%B0%83%E7%94%A8%E8%BF%87%E7%A8%8B"><span class="nav-number">2.2.</span> <span class="nav-text">Dubbo的调用过程</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B3%E4%BA%8E%E7%BD%91%E5%85%B3"><span class="nav-number">3.</span> <span class="nav-text">关于网关</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A6%82%E4%BD%95%E6%8B%BF%E5%88%B0%E6%B3%A8%E5%86%8C%E4%BF%A1%E6%81%AF"><span class="nav-number">3.1.</span> <span class="nav-text">如何拿到注册信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%E6%B3%9B%E5%8C%96%E8%B0%83%E7%94%A8"><span class="nav-number">3.2.</span> <span class="nav-text">如何使用泛化调用</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">RunningEgg</p>
  <div class="site-description" itemprop="description">写下我的点点滴滴，写下我对这个世界的看法，还有我内心锁崇拜的技术</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">89</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">24</span>
        <span class="site-state-item-name">tags</span>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="mailto:guodh_1@163.com" title="E-Mail → mailto:guodh_1@163.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">RunningEgg</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> v5.3.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">Theme – <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> v7.7.1
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  















  

  

</body>
</html>
